"""
利用pandas 的 DataFrame or Series
指标模板
指标的计算方式分为2类：
1、基于序列的
2、基于单点的

建议：
测试时尽量用序列方式，而实盘时用单点方式
"""

from datetime import datetime, date
import pandas as pd


class IndiClcType:
    """指标计算方式"""
    SERIES = 'Series'  # 基于序列进行计算
    TICK = 'Tick'  # 按数据点进行计算


class GraphPosition:
    """画线时在界面上的位置"""
    MAIN = 0  # 主图
    ONE = 1  # 第{ONE}副图
    TWO = 2  # 第2副图
    THREE = 3  # 第3副图
    FOUR = 4  # 第3副图
    FIVE = 5  # 第3副图
    SIX = 6  # 第3副图
    SEVEN = 7  # 第3副图


class Tick:
    d = ''
    price = 0.0
    vol = 0
    askp = 0.0
    askv = 0
    bidp = 0.0
    bidv = 0
    ap = 0.0


class Bar:
    d = ''
    o = h = l = c = 0.0
    v = 0
    a = 0.0


class BaseInd:
    def __init__(self, params: dict = None):
        self.params = {}
        if params:
            self.update_params(params)

        self.output = {}
        self.tick = None
        self.ticks = None
        self.bar = None
        self.bars = None
        self.period = 0

    def on_tick(self, tick):
        """

        :param tick:
        :return:
        """

    def on_ticks(self, ticks):
        """

        :param ticks:
        :return:
        """

    def on_bar(self, bar):
        """

        :param bar:
        :return:
        """

    def on_bars(self, bars):
        """

        :param bars:
        :return:
        """

    def update_params(self, params: dict):
        for k, v in params.items():
            self.params[k] = v
            setattr(self, k, v)

    @property
    def last_price(self):
        if self.tick is None:
            if self.ticks is None or len(self.ticks) == 0:
                return None
            else:
                self.tick = self.ticks.iloc[-1]
                return self.ticks.iloc[-1]['last_price']
        return self.tick['last_price']

    @property
    def last_prices(self):
        if self.ticks is None:
            return None
        return self.ticks['last_price']

    @property
    def ask_price(self):
        if self.tick is None:
            return None
        return self.tick['ask_price1']

    @property
    def ask_vol(self):
        if self.tick is None:
            return None
        return self.tick['ask_volume1']

    @property
    def bid_price(self):
        if self.tick is None:
            return None
        return self.tick['bid_price1']

    @property
    def bid_vol(self):
        if self.tick is None:
            return None
        return self.tick['bid_volume1']

    @property
    def datetime(self):
        if self.tick is None:
            if self.ticks is None or len(self.ticks) == 0:
                return None
            else:
                self.tick = self.ticks.iloc[-1]
        dt = self.tick['datetime']
        return dt if isinstance(dt, (datetime, date)) else datetime.fromtimestamp(dt / 1e9).strftime(
            '%Y-%m-%d %H:%M:%S') + '.' + str(int(dt % 1e9))[0:3]

    @property
    def datetimes(self):
        if self.tick is None:
            return None
        dt = self.ticks['datetime']
        return pd.Series([datetime.fromtimestamp(dt.iloc[i] / 1e9).strftime(
            '%Y-%m-%d %H:%M:%S') + '.' + str(int(dt.iloc[i] % 1e9))[0:3] for i in len(dt)])

    lp = last_price
    lps = last_prices
